﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YJKRevitBase.WinForm;
using YJKRevitView;
using YJKRevitWallModule.WallModule.SearchExternalLoop;

namespace YJKRevitWallModule.WallModule
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdWallCreateByAxis : YJKCommandManual
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)11;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			this.m_data = new WallData();
			if (CmdWallCreateByAxis._hWndRevit == null)
			{
				CmdWallCreateByAxis._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			Document document = cmdData.Application.ActiveUIDocument.Document;
			DlgAxisToWall dlgAxisToWall = null;
			try
			{
				dlgAxisToWall = new DlgAxisToWall(cmdData);
				dlgAxisToWall.Show(CmdWallCreateByAxis._hWndRevit);
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
				if (dlgAxisToWall != null)
				{
					dlgAxisToWall.Close();
				}
				return Autodesk.Revit.UI.Result.Failed;
			}
			int wMsg = 7;
			CmdWallCreateByAxis.SendMessage(CmdWallCreateByAxis._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			Result result;
			for (;;)
			{
				Transaction transaction = new Transaction(this.m_doc, "axis to wall");
				transaction.Start();
				AxisFilter axisFilter = new AxisFilter();
				List<Element> list = new List<Element>();
				try
				{
					foreach (Reference reference in this.m_uiDoc.Selection.PickObjects((Autodesk.Revit.UI.Selection.ObjectType)1, axisFilter, "请选择轴网："))
					{
						list.Add(this.m_doc.GetElement(reference));
					}
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException)
				{
					transaction.RollBack();
					dlgAxisToWall.Close();
					result = Autodesk.Revit.UI.Result.Succeeded;
					break;
				}
				catch (Exception ex2)
				{
					message = ex2.Message;
					transaction.RollBack();
					dlgAxisToWall.Close();
					result = Autodesk.Revit.UI.Result.Failed;
					break;
				}
				this.DisconnectMultiSegmentGrid(cmdData.Application.ActiveUIDocument.Document, ref list);
				AssistFunc.CompressArray(list);
				dlgAxisToWall.GetWallData(ref this.m_data);
				List<Curve> list2 = new List<Curve>();
				foreach (Element element in list)
				{
					Grid grid = element as Grid;
					list2.Add(grid.Curve);
				}
				List<Curve> list3 = new List<Curve>();
				foreach (Curve curve in list2)
				{
					List<Curve> collection = WallCreatorByCurve.FormatCurve(curve, list2, this.m_doc, false);
					list3.AddRange(collection);
				}
				LoopSearcher loopSearcher = new LoopSearcher(cmdData);
				List<Curve> curveList = new List<Curve>();
				List<Curve> edgeLoopsByEdgeList = loopSearcher.GetEdgeLoopsByEdgeList(list3, ref curveList);
				WallCreatorByCurve wallCreatorByCurve = new WallCreatorByCurve(this.m_uiApp, this.m_doc);
				List<List<Wall>> list4 = wallCreatorByCurve.CreateWallByCurve(edgeLoopsByEdgeList, this.m_data.OutType, this.m_data.IdOutLevel, this.m_data.OutOffset, this.m_data.OutWallHeight, dlgAxisToWall.cbxBreak.Checked);
				List<List<Wall>> list5 = wallCreatorByCurve.CreateWallByCurve(curveList, this.m_data.InType, this.m_data.IdInLevel, 0.0, this.m_data.InsideWallHeight, dlgAxisToWall.cbxBreak.Checked);
				if (list4.Count == 0 && list5.Count == 0)
				{
					YJKMessageBox.Show("所选区域无法生成墙体！");
				}
				transaction.Commit();
				wallCreatorByCurve.JoinLinkWalls(list4);
				wallCreatorByCurve.JoinLinkWalls(list5);
			}
			return result;
		}

		private void DisconnectMultiSegmentGrid(Autodesk.Revit.DB.Document doc, ref List<Element> arAxis)
		{
			List<Element> list = new List<Element>();
			for (int i = 0; i < arAxis.Count; i++)
			{
				Element element = arAxis[i];
				if (!(element.GetType() == typeof(MultiSegmentGrid)) && element.GetType() == typeof(Grid))
				{
					list.Add(element);
				}
			}
			arAxis.Clear();
			arAxis.AddRange(list);
		}

		public static WindowHandle _hWndRevit;

		public WallData m_data;
	}
}
